/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO;

import DTO.CFields;
import DTO.CChiTietHoaDon;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
public class CChiTietHoaDonDAO extends IData<CChiTietHoaDon>{
    public CChiTietHoaDonDAO(){}

    @Override
    protected Object OnSubmit(CChiTietHoaDon item, int t, SubConfig config) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    protected CChiTietHoaDon getItem(ResultSet rs) {
        CChiTietHoaDon item = new CChiTietHoaDon();
        try {
            item.setId(rs.getLong(CFields.id));
            item.setDongia(rs.getDouble(CFields.don_gia));
            item.setGiamGia(rs.getDouble(CFields.giam_gia));
            item.setSanPhamId(rs.getLong(CFields.san_pham_id));
            item.setTang(rs.getBoolean(CFields.isTang));
            item.setThanhTien(rs.getDouble(CFields.thanh_tien));
            
        } catch (SQLException ex) {
            Logger.getLogger(CChiTietHoaDonDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return item;
    }

    @Override
    public List<CChiTietHoaDon> getAll(SubConfig config) {
        String mysql = "select * from " + CFields.tb_chi_tiet_hoa_don + " " + this.sWhere;
        return this.getAll(mysql, config);
    }

    @Override
    public Object insertOnSubmit(CChiTietHoaDon item, SubConfig config) {
        Object result  = 0;
        String mysql = "insert into " + CFields.tb_chi_tiet_hoa_don +"("
                + CFields.hoa_don_id + "," + CFields.san_pham_id + ","
                + CFields.don_vi + "," + CFields.so_luong +"," 
                + CFields.don_gia + "," + CFields.thanh_tien + ","
                + CFields.giam_gia + "," + CFields.tien_tra  
                +") values(?,?,?,?,?,?,?,?)";
        System.out.println("" + mysql);
        try {
            PreparedStatement cs = config.connec.prepareStatement(mysql,PreparedStatement.RETURN_GENERATED_KEYS);
            cs.setLong(1, item.getHoaDonId());
            cs.setLong(2, item.getSanPhamId());
            cs.setString(3, item.getDonVi());
            cs.setInt(4, item.getSoLuong());
            cs.setDouble(5, item.getDongia());
            cs.setDouble(6, item.getThanhTien());
            cs.setDouble(7, item.getGiamGia());
            cs.setDouble(8, item.getTienTra());
            //cs.setLong(9, item.getId());
            cs.executeUpdate();
            ResultSet keyResultSet = cs.getGeneratedKeys();
            if (keyResultSet.next()) {
                result = (long) keyResultSet.getInt(1);
            }
        } catch (SQLException ex) {
            Logger.getLogger(CChiTietHoaDonDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return result;
    }

    @Override
    public Object updateOnSubmint(CChiTietHoaDon item, SubConfig config) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public Object deleteOnSubmint(CChiTietHoaDon item, SubConfig config) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

   

    @Override
    public CChiTietHoaDon Find(CChiTietHoaDon item, List<CChiTietHoaDon> list) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public CChiTietHoaDon add(CChiTietHoaDon item) {
        try {
            ps.setLong(1, item.getHoaDonId());
            ps.setLong(2, item.getSanPhamId());
            ps.setString(3, item.getDonVi());
            ps.setInt(4, item.getSoLuong());
            ps.setDouble(5, item.getDongia());
            ps.setDouble(6, item.getThanhTien());
            ps.setDouble(7, item.getGiamGia());
            ps.setDouble(8, item.getTienTra());
           // ps.setLong(9, item.getId());
            ps.addBatch();
        } catch (SQLException ex) {
            Logger.getLogger(CChiTietHoaDonDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return item;
    }

    @Override
    public void saveBatch() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void addrows(List<CChiTietHoaDon> list, SubConfig config) {
        String mysql = "insert into " + CFields.tb_chi_tiet_hoa_don +"("
                + CFields.hoa_don_id + "," + CFields.san_pham_id + ","
                + CFields.don_vi + "," + CFields.so_luong +"," 
                + CFields.don_gia + "," + CFields.thanh_tien + ","
                + CFields.giam_gia + "," + CFields.tien_tra 
                +") values(?,?,?,?,?,?,?,?)";
        System.out.println("" + mysql);
        try {
            ps = config.connec.prepareStatement(mysql);
            for(CChiTietHoaDon item : list){
                this.add(item);
            }
            int []n = ps.executeBatch();
            ps.close();
        } catch (SQLException ex) {
            Logger.getLogger(CChiTietHoaDonDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
}
